﻿require(["esri/map",
		"esri/layers/ArcGISDynamicMapServiceLayer",
		"esri/dijit/Scalebar",
		"esri/geometry/Extent",
		"esri/SpatialReference",
		"esri/geometry/Polyline",
		"esri/geometry/Polygon",
		"esri/symbols/SimpleLineSymbol",
		"esri/symbols/SimpleFillSymbol",
		"esri/graphic",
		"esri/layers/GraphicsLayer",
		"esri/Color",
		"esri/tasks/AreasAndLengthsParameters",
		"esri/tasks/GeometryService",
		"dojo/request",
		"dojo/domReady!"],
	function (Map, ArcGISDynamicMapServiceLayer, Scalebar, Extent,
		SpatialReference, Polyline, Polygon, SimpleLineSymbol,
		SimpleFillSymbol, Graphic, GraphicsLayer, AreasAndLengthsParameters,
		GeometryService, Color, Request) {

	/**
	 * 定位
	 */
	var mapextent = new Extent({
			"xmin": 70.171,
			"ymin": 31.506,
			"xmax": 137.109,
			"ymax": 31.506,
			"spatialReference": {
				"wkid": 102100
			}
		});

	var map = new Map("mapDiv", {
			extent: mapextent,
			logo: false
		});

	var scalebar = new Scalebar({
			map: map,
			attachTo: "bottom-left", //比例尺位置
			scalebarUnit: "metric" //比例尺单位（metric米，默认english）
		});

	var url = "http://192.168.20.117:6080/arcgis/rest/services/demo/mymap/MapServer";
	//"http://192.168.20.68:6080/arcgis/rest/services/testmap/MapServer";//

	var layer = new ArcGISDynamicMapServiceLayer(url);
	map.addLayer(layer);
	scalebar.show();

	console.log("map level is " + map.getLevel());
	console.log("map zoom is " + map.getZoom());
	console.log("map scale is " + map.getScale());

	/**
	 * 标记层
	 */
	var graphicsLayer = new GraphicsLayer({
			opacity: 0.20
		});

	var makeSimpleLineSymbolGraphic = function () {

		var polyline = new Polyline({
				"paths": [[[100.046, 31.802], [102.136, 31.736], [100.443, 30.466], [100.046, 31.802]]],
				"spatialReference": {
					"wkid": 102100
				}
			});
		var symbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASHDOT, new Color([255, 0, 0]), 2);

		return new Graphic(polyline, symbol);
	};

	//显示标记操作
	dojo.query("#showLineSymbolMarkbtn").on("click", function () {
		graphicsLayer.add(makeSimpleLineSymbolGraphic());
		map.addLayer(graphicsLayer);
	});

	//删除标记操作
	dojo.query("#deleteLineSymbolMarkbtn").on("click", function () {
		map.removeLayer(graphicsLayer);
	});

	/**
	 * 计算测量结果：长度/面积
	 */
	var measurePolygonResults = function (polygons) {
		var geometryService = new GeometryService();
		var areasAndLengthParams = new AreasAndLengthsParameters();
		areasAndLengthParams.lengthUnit = GeometryService.UNIT_METER;
		areasAndLengthParams.areaUnit = GeometryService.UNIT_SQUARE_METERS;
		areasAndLengthParams.polygons = polygons;
		geometryService.areasAndLengths(areasAndLengthParams);
	};

	/**
	 * 填充
	 */
	var fillsymbolgraphicslayer = new GraphicsLayer({
			opacity: 0.20
		});

	dojo.query("#showFillSymbolMarkbtn").on("click", function () {

		var fillData = function (data) {
			var sfs = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
					new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASHDOT, new Color([255, 0, 0]), 2),
					new Color([255, 255, 0, 0.25]));

			var polygon = new Polygon({
					"rings": data.rings,
					"spatialReference": {
						"wkid": data.wkid
					}
				});

			//measurePolygonResults(polygon);

			var graphic = new Graphic(polygon, sfs);

			fillsymbolgraphicslayer.add(graphic);
			map.addLayer(fillsymbolgraphicslayer);
		};

		dojo.xhrGet({
			url: "/DataProvider?k=polygon",
			handleAs: "json",
			handle: function (data) {
				fillData(data);
			}
		});

	});

	dojo.query("#deleteFillSymbolMarkbtn").on("click", function () {
		map.removeLayer(fillsymbolgraphicslayer);
	});

});